package com.jt.serviceImpl;

import com.jt.pojo.Order;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * @author dc
 * @Classname OrderConsumer
 * @Date 2020/3/14 12:12
 */
@Component
public class OrderConsumer {
    /**
     *  收到订单数据后,会调用订单的业务代码,把订单保存到数据库
     */
    @Autowired
    private OrderServiceImpl orderService;

    //添加该注解后,会从指定的orderQueue接收消息,
    //并把数据转为 Order 实例传递到此方法
    @RabbitListener(queues="orderQueue")
    public void save(Order order, Channel channel, Message message)
    {
        try {
            orderService.saveOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
